local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.GameObject(),script.Parent.Name.."_"..script.Name);
local ConnectClass=CommonOHOH.load.Connect();
local WeapCfg=_G.CommonOHOH.config.Weapon
local EquipFolderName="Equip_Weapon"
local Tool=_G.CommonOHOH.load.WeaponTool();

function M.new(self, ...)
	return M(self, ...);
end;

-- 构造
function M.ctor(self,equiId,character,obj,player)
    local WeaponObj=obj --武器模型
    if WeaponObj then
        WeaponObj.Name=equiId --武器名改为装备Id
    else
        WeaponObj=M.FindWeaponByEquipId(character,equiId)
    end

    self.player=player
    self.equiId=equiId --装备Id
    self.character=character --装备的对象
    local cfgId,level=Tool.GetCfgIdAndLevel(equiId)
   
    self.cfgId=cfgId -- 武器配置Id
    self.level=level --武器等级

    self.cfg=WeapCfg[self.cfgId] -- 武器配置表数据

    self.Damge=Tool.Calculate_power(self.equiId) --武器基础伤害

    self.baseCd=self.cfg.cd --攻击间隔
    self.StartAttackEvent=ConnectClass.new() --开始攻击
    self.AttackDoneEvent=ConnectClass.new() --攻击完成

    self.Syn2ServerEvent=ConnectClass.new() --同步到服务器事件
    self.Syn2ClientEvent=ConnectClass.new() --同步给其它客户端事件

    
    self.PushDamgeEvent=ConnectClass.new()-- 产生伤害事件
    self.FindTagEvent=ConnectClass.new()-- 寻找目标
    self.AttackCdEvent=ConnectClass.new()-- 获取攻击CD
    self.AttackDamgeEvent=ConnectClass.new()-- 获取攻击力

    M.super.ctor(self,WeaponObj);
    
    if obj then
        self:SetParentWeaponFolder()
        self:Bind2character()
    end
    -- ColliderManager:SetAllGroup(self.main_inst, ColliderGroup.NoCollider);
end

-- 析构
function M.dtor(self)

    self.StartAttackEvent:dtor()
    self.AttackDoneEvent:dtor()

    self.Syn2ServerEvent:dtor()
    self.Syn2ClientEvent:dtor()

    self.PushDamgeEvent:dtor()
    self.FindTagEvent:dtor()
    self.AttackCdEvent:dtor()
    self.AttackDamgeEvent:dtor()

    local obj= self.main_inst
    -- self.main_inst:Destroy()

	M.super.dtor(self);

    obj:Destroy()


end;

function M.GetBaseCd(self)
    return self.baseCd
end


function M.GetBaseDamge(self)
    return self.Damge
end

function M.GetLevel(self)
    return self.level
end

function M.GetCfg(self)
    return self.cfg
end


--获取武器装备文件夹
function M.GetWeaponFolder(self)
    -- print("------GetWeaponFolder-----self.WeaponFolder---",self.character.main_inst.Name)
    if   self.WeaponFolder then
        return  self.WeaponFolder
    end
    self.WeaponFolder=  self.character.main_inst:FindFirstChild(EquipFolderName)
    if not  self.WeaponFolder then
        self.WeaponFolder= Instance.new("Folder", self.character.main_inst)
        self.WeaponFolder.Name=EquipFolderName
    end

    return  self.WeaponFolder
end

--将武器 放入装备文件夹
function M.SetParentWeaponFolder(self)
    self.main_inst.Parent=self:GetWeaponFolder()
end

-- 根据装备ID，寻找装备
function M.FindWeaponByEquipId(self,equipId)
  return    CommonOHOH:GetPath_Loop(EquipFolderName.."."..tostring(equipId),self.main_inst)
end


function M.UpdateLevelByEquipId(self,equiId)
    self.equiId=equiId
    local cfgId,level=Tool.GetCfgIdAndLevel(equiId)
    self.level=level
    self.Damge=Tool.Calculate_power(self.equiId) --武器基础伤害
end

--获取装备Id
function M.GetEquipId(self)
    return self.equiId
end

--获取配置表中的Id
function M.GetWeaponId(self)
    return self.cfgId
end

--通过装备Id判断两把武器是否一样
function M.IsEqual(self,equiId)
    local cfgId,level=Tool.GetCfgIdAndLevel(equiId)
    return cfgId==self.cfgId
end

--卸载
function M.UnEquip(self)
    self:dtor()
end

-- 开始攻击
function M.StartAttack(self,...)
    self.StartAttackEvent:Action()
end


-------------------非常--重要-------以下为伤害接口-使用者必须监听----------非常--重要-------------

-- 获取攻击cd
function M.GetAttackCd(self)
    local list=  self.AttackCdEvent:Action(self.baseCd)
    local sum=0
    for _,v in pairs(list) do
        sum=sum+v
    end
    return sum
end

-- 获取攻击力
function M.GetDamge(self)
    local list=  self.AttackDamgeEvent:Action(self.Damge)
    local sum=0
    for _,v in pairs(list) do
        sum=sum+v
    end
    return sum
end

--获取攻击目标
function M.GetTagInfo(self,...)
    local list= self.FindTagEvent:Action(...)
    for _,v in pairs(list) do
        return v
    end
end


--对对象产生伤害
function M.PushDamge(self,...)
    self.PushDamgeEvent:Action(...)
end


--装备到玩家身上处理
function M.Bind2character(self)
   
    if  self.main_inst:FindFirstChild("rightHand") then
        self:AddToRightHand()
    elseif  self.main_inst:FindFirstChild("leftHand") then
        self:AddToLeftHand()
    end
end


--广播给其它客户端
function M.Syn2Client(self,...)
    self.Syn2ClientEvent:Action(...)
end


--同步到服务器
function M.Syn2Server(self,...)
    self.Syn2ServerEvent:Action(...)
end




function M.AddToLeftHand(self)
    local cf=self.character.main_inst.LeftHand.CFrame
    self.main_inst.CFrame=cf:ToWorldSpace(self.main_inst.leftHand.CFrame)
    local w=Instance.new("WeldConstraint",self.main_inst)
    w.Part0=self.main_inst
    w.Part1=self.character.main_inst.LeftHand
    w.Name=self.equiId
end

function M.AddToRightHand(self)
    local cf=self.character.main_inst.RightHand.CFrame
    self.main_inst.CFrame=cf:ToWorldSpace(self.main_inst.rightHand.CFrame)
    local w=Instance.new("WeldConstraint",self.main_inst)
    w.Part0=self.main_inst
    w.Part1=self.character.main_inst.RightHand
    w.Name=self.equiId
end


return M;
